<template>
  <div>
    <el-dialog
      v-model="open"
      width="50%"
      append-to-body
      class="dialogClass"
      
    >
      <template #title>
        <el-icon color="#fff"><CollectionTag /></el-icon>
        <span>试卷详情</span>
      </template>

      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button color="#265DA4" plain icon="Plus" @click="handleAdd"
          v-hasPermi="['manager:paperQuestion:add']"
            >新增</el-button
          >
        </el-col>
      </el-row>
      <el-table v-loading="loading" :data="dataList">
        <!-- <el-table-column
          label="序号"
          align="left"
          type="index"
          width="100"
        ></el-table-column> -->
        <el-table-column type="expand">
          <template #default="props">
            <div m="10">
              <h3>选项</h3>
              <el-table :data="props.row.answerList">
                <el-table-column label="序号" type="index" width="100" />
                <el-table-column label="选项" prop="answSerial" />
                <el-table-column label="内容" prop="answContent" />
              </el-table>
            </div>
          </template>
        </el-table-column>
        <el-table-column
          label="题目名称"
          align="left"
          prop="questionTitle"
        ></el-table-column>
        <el-table-column
          label="考题类型"
          align="left"
          prop="libTypeName"
          width="200"
        ></el-table-column>
        <el-table-column label="分值" align="left" prop="quesScore" width="100">
        </el-table-column>
        <!-- <el-table-column label="答案" align="left" prop="correctAnswer" width="100">
        </el-table-column> -->
        <el-table-column
          label="操作"
          align="left"
          class-name="small-padding fixed-width"
        >
          <template #default="scope">
            <el-button
              link
              color="#265DA4"
              icon="Edit"
              @click="handleEdit(scope.row)"
              v-hasPermi="['manager:stashQuestion:edit']"
              >修改</el-button
            >
            <el-button
              link
              color="#265DA4"
              icon="Delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['manager:stashQuestion:remove']"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="total > 0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getListData"
      />
      <template #footer>
        <div class="dialog-footer">
          <!-- <el-button color="#265DA4">确 定</el-button> -->
          <el-button @click="open = false">关 闭</el-button>
        </div>
      </template>
    </el-dialog>

    <!-- 新增/编辑题目弹框 -->
    <el-dialog
      v-model="openAdd"
      width="45%"
      append-to-body
      class="dialogClass"
      
    >
      <template #title>
        <el-icon color="#fff"><CollectionTag /></el-icon>
        <span>{{ mode === "add" ? "新增" : "编辑" }}</span>
      </template>
      <el-form
        :model="form"
        :rules="rules"
        ref="formRef"
        :inline="true"
        v-if="mode === 'edit'"
      >
        <el-form-item label="分值：" prop="score">
          <el-input
            v-model="form.score"
            placeholder="请输入试卷名称"
            clearable
            style="width: 200px"
          />
        </el-form-item>
      </el-form>
      <el-form :model="queryParamsQ" ref="queryRef" :inline="true">
        <el-form-item label="考题题目" prop="quesTitle">
          <el-input
            v-model="queryParamsQ.quesTitle"
            placeholder="请输入考题题目"
            clearable
            style="width: 200px"
          />
        </el-form-item>
        <el-form-item label="考题类型:" prop="quesType">
          <el-select
            v-model="queryParamsQ.quesType"
            clearable
            placeholder="请选择考题类型"
          >
            <el-option
              v-for="dict in quesTypeList"
              :key="dict.dictCode"
              :label="dict.dictLabel"
              :value="dict.dictCode"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button color="#265DA4" :dark="isDark" icon="Search" @click="handleQuery"
            >搜索</el-button
          >
        </el-form-item>
      </el-form>
      <el-table :data="questionList">
        <el-table-column label="序号" align="left" type="index" width="100" />
        <el-table-column label="题目名称" align="left" prop="quesTitle" />
        <el-table-column label="考题类型" align="left" prop="quesTypeName" />
        <el-table-column
          label="分值"
          align="left"
          prop="quesScore"
          width="100"
        />
        <el-table-column
          label="操作"
          align="left"
          class-name="small-padding fixed-width"
        >
          <template #default="scope">
            <el-button link style="color:#265DA4;" @click="selectItem(scope.row)"
              >选择</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="totalQ > 0"
        :total="totalQ"
        v-model:page="queryParamsQ.pageNum"
        v-model:limit="queryParamsQ.pageSize"
        @pagination="getListQ"
      />
    </el-dialog>
  </div>
</template>

<script setup>
const props = defineProps({
  paperId: {
    type: Number,
    default: "",
  },
});

import { questionTypeList } from "@/api/question/classification";
import {
  paperQuestionList,
  paperQuestionAdd,
  paperQuestionrEditor,
  paperQuestionEdit,
  paperQuestionRomove,
} from "@/api/examManage/examPaper";
import { stashQuestionList } from "@/api/question/question";
import { reactive } from "vue";
const { proxy } = getCurrentInstance();
const open = ref(false);
const openAdd = ref(false);
const loading = ref(false);
const dataList = ref([]);
const quesTypeList = ref([]);
const questionList = ref([]);
const total = ref(0);
const totalQ = ref(0);
const mode = ref("add");
const editform = ref({});


const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    paperId: props.paperId,
  },
  queryParamsQ: {
    pageSize: 10,
    pageNum: 1,
    quesType: "",
    quesTitle: "",
  },
  form: {
    score: "",
  },
  rules: {
    score: [{ required: true, message: "请输入分值", trigger: "blur" }],
  },
});

const { form, queryParams, queryParamsQ, rules } = toRefs(data);

/** 查询列表 */
function getQuesList() {
  stashQuestionList(queryParamsQ.value).then((response) => {
    questionList.value = response.rows;
    totalQ.value = response.total;
  });
}
// 考题类型
function getquestionTList() {
  questionTypeList(queryParamsQ.value).then((response) => {
    quesTypeList.value = response.rows;
  });
}
/** 查询试卷列表 */
function getList(paperId) {
  console.log(paperId, "paperId", props.paperId);
  loading.value = true;
  paperQuestionList({
    ...queryParams.value,
    paperId,
  }).then((response) => {
    dataList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}
function getListData(val) {
  console.log(queryParams, "value", "queryParams");
  queryParams.value.pageNum = val.page;
  queryParams.value.pageSize = val.limit;
  getList(props.paperId);
}
function getListQ(val) {
  queryParamsQ.value.pageNum = val.page;
  queryParamsQ.value.pageSize = val.limit;
  getQuesList();
}
/** 新增按钮操作 */
function handleAdd() {
  openAdd.value = true;
  mode.value = "add";
  editform.value.paperId = props.paperId;
}
// 编辑
function handleEdit(row) {
  openAdd.value = true;
  mode.value = "edit";
  editform.value.id = row.id;
  editform.value.paperId = row.paperId;
}
function handleDelete(row) {
  console.log(row, "row----");
  editform.value.paperId = row.paperId;
  proxy.$modal
    .confirm("是否确认该数据项？")
    .then(function () {
      return paperQuestionRomove(row.id);
    })
    .then(() => {
      getList(editform.value.paperId);
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {});
}
// 选择题目
function selectItem(row) {
  console.log(row, "row");
  editform.value.questionId = row.quesId;
  console.log(editform.value, "editform.value");
  if (mode.value == "add") {
    // "paperId": 1710576203009040386,
    // "questionId": 1710573573226692610
    paperQuestionAdd({
      paperId: editform.value.paperId,
      questionId: editform.value.questionId,
    }).then((response) => {
      proxy.$modal.msgSuccess(response.msg);
      getList(editform.value.paperId);
      openAdd.value = false;
    });
  } else {
    paperQuestionEdit(editform.value).then((response) => {
      proxy.$modal.msgSuccess(response.msg);
      getList(editform.value.paperId);
      openAdd.value = false;
    });
  }
}
// 搜索
function handleQuery() {
  queryParamsQ.value.pageNum = 1;
  getQuesList();
}

getQuesList();
getquestionTList();
defineExpose({
  open,
  getList,
});
</script>

<style lang="scss" scoped>
</style>
